package com.yanqu.road.dao.impl.nenmonopolymarket.office;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.office.NewMonopolyMarketOfficeFinishData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NewMonopolyMarketOfficeFinishDataDaoImpl extends TempDataObjectDao<NewMonopolyMarketOfficeFinishData> {

    public long getMaxId(){
        String sql = "select ifnull(max(id),0) as maxId from t_c_new_monopoly_market_office_finish";
        ResultSet rs = null;
        CallableStatement call = null;
        long maxId = 0L;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }

    public static String getNewMonopolyMarketOfficeFinishDataSql = "select * from `t_c_new_monopoly_market_office_finish` where `weekId`=? and `groupId`=? and `isSyn`=0";
    public Map<Long, NewMonopolyMarketOfficeFinishData> getNewMonopolyMarketOfficeFinishData(int weekId, long groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, NewMonopolyMarketOfficeFinishData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getNewMonopolyMarketOfficeFinishDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            call.setLong(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                NewMonopolyMarketOfficeFinishData data =  parse(rs);
                map.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateNewMonopolyMarketOfficeFinishDataSql = "update `t_c_new_monopoly_market_office_finish` set weekId=?,groupId=?,userId=?,posIndex=?,level=?,no=?,robName=?,isSyn=?,time=?,finishType=? where id=?";
    @Override
    protected boolean updateData(NewMonopolyMarketOfficeFinishData newMonopolyMarketOfficeFinishData) {
        newMonopolyMarketOfficeFinishData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketOfficeFinishData.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketOfficeFinishData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketOfficeFinishData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketOfficeFinishData.getPosIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketOfficeFinishData.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketOfficeFinishData.getNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, newMonopolyMarketOfficeFinishData.getRobName()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketOfficeFinishData.getIsSyn()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketOfficeFinishData.getTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketOfficeFinishData.getFinishType()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketOfficeFinishData.getId()));
        if (getDb().executeNoneQuery(updateNewMonopolyMarketOfficeFinishDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertNewMonopolyMarketOfficeFinishDataSql = "insert into `t_c_new_monopoly_market_office_finish`(id,weekId,groupId,userId,posIndex,level,no,robName,isSyn,time,finishType) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(NewMonopolyMarketOfficeFinishData newMonopolyMarketOfficeFinishData) {
        newMonopolyMarketOfficeFinishData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketOfficeFinishData.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketOfficeFinishData.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketOfficeFinishData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketOfficeFinishData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketOfficeFinishData.getPosIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketOfficeFinishData.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketOfficeFinishData.getNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, newMonopolyMarketOfficeFinishData.getRobName()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketOfficeFinishData.getIsSyn()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketOfficeFinishData.getTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketOfficeFinishData.getFinishType()));
        if (getDb().executeNoneQuery(insertNewMonopolyMarketOfficeFinishDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected NewMonopolyMarketOfficeFinishData parseData(ResultSet rs) throws SQLException {
        NewMonopolyMarketOfficeFinishData newMonopolyMarketOfficeFinishData = new NewMonopolyMarketOfficeFinishData();
        newMonopolyMarketOfficeFinishData.setId(rs.getLong("id"));
        newMonopolyMarketOfficeFinishData.setWeekId(rs.getInt("weekId"));
        newMonopolyMarketOfficeFinishData.setGroupId(rs.getLong("groupId"));
        newMonopolyMarketOfficeFinishData.setUserId(rs.getLong("userId"));
        newMonopolyMarketOfficeFinishData.setPosIndex(rs.getInt("posIndex"));
        newMonopolyMarketOfficeFinishData.setLevel(rs.getByte("level"));
        newMonopolyMarketOfficeFinishData.setNo(rs.getInt("no"));
        newMonopolyMarketOfficeFinishData.setRobName(rs.getString("robName"));
        newMonopolyMarketOfficeFinishData.setIsSyn(rs.getBoolean("isSyn"));
        newMonopolyMarketOfficeFinishData.setTime(rs.getInt("time"));
        newMonopolyMarketOfficeFinishData.setFinishType(rs.getInt("finishType"));
        return newMonopolyMarketOfficeFinishData;
    }
}
